package com.example.wilson.lesson12;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button myButton = null;
    private final String nobody = "1";
    private Button registerButton = null;
    private Button unregisterButton = null;
    private SMSReceiverActivity smsReceiver = null;

    private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText phoneNum = (EditText)findViewById(R.id.phoneNum);
        final Button button = (Button)findViewById(R.id.Button01);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String call = phoneNum.getText().toString();
                if(PhoneNumberUtils.isGlobalPhoneNumber(call)){
                    Intent I = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://" + call));
                    startActivity(I);
                }
                else {
                    Toast.makeText(MainActivity.this, "你个傻逼，错了", Toast.LENGTH_LONG ).show();
                }
            }
        });

        /*
        Button dialer = (Button)findViewById(R.id.Button01);
        dialer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://114"));
                startActivity(intent);
            }
        });
        */

        myButton = (Button)findViewById(R.id.Button02);
        myButton.setOnClickListener( new MyButtonListener());

        registerButton = (Button)findViewById(R.id.registerButton);
        registerButton.setOnClickListener(new RegisterReceiverListener());
        unregisterButton = (Button)findViewById(R.id.unregisterButton);
        unregisterButton.setOnClickListener(new UnRegisterReceiverListener());
    }

    class RegisterReceiverListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //生成一个BroiadcastReceiver对象
            smsReceiver = new SMSReceiverActivity();
            //生成一个IntentFilter对象
            IntentFilter filter = new IntentFilter();
            //为IntentFilter添加一个Action
            filter.addAction(SMS_ACTION);
            //将BroadcastReceiver对象注册到系统当中
            MainActivity.this.registerReceiver(smsReceiver, filter);
        }
    }

    class UnRegisterReceiverListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //解除BroadcastReceiver对象的注册
            MainActivity.this.unregisterReceiver(smsReceiver);
        }
    }

    class MyButtonListener implements OnClickListener{


        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "asbjfkcs", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(nobody);

            intent.putExtra("testInent", "1111111");
            sendBroadcast(intent);
        }
    }
}
